home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ETO Development Tools 4
/
ETO Development Tools 4.iso
/
Tools - Objects
/
MacApp
/
MacApp 2.0.1
/
MacApp CD Release
/
MacApp® 2.0.1 Tutorial
/
Chapter 10
/
UIconEdit.inc1.p
< prev
next >
Wrap
Text File
|
1990-10-25
|
11KB
|
390 lines
{Copyright © 1989 by Apple Computer, Inc. All rights reserved.}
CONST
kIconHBits = 32; { Number of horizontal bits in a bitmap.}
kIconVBits = 32; { Number of vertical bits in a bitmap. }
kIconSizeInBytes = kIconHBits * kIconVBits DIV 8; { Number of bytes in an bitmap. }
kIconSizeInLongs = kIconSizeInBytes DIV 4; { Number of long words in a bitmap. }
kMaxLong = kIconSizeInLongs - 1; { Max. addressable long word in bitmap. }
{ Resource identifiers }
kSeedIconId = 1000; { Id of the seed icon resource. }
kIconWindowId = 1000; { Id of the icon window 'view' resource.}
kIconViewId = 1001; { Id of the TIconView resource. }
{ Constants for TIconView }
kDefaultMagnification = 7; { Default icon magnification. }
kBorder = 5; { Border in which to inset drawing. }
TYPE
LongArrayHdl = ^LongArrayPtr;
LongArrayPtr = ^LongArray;
LongArray = ARRAY [0..kMaxLong] OF LONGINT;
{-------------------------------------------------------------------------------------------}
{--------------------------------TIconApplication methods-----------------------------------}
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconApplication.IIconApplication(iconFileType: OSType);
VAR anIconView : TIconView;
BEGIN
IApplication(iconFileType);
if gCreateWithTemplates then begin { Make sure the linker doesn't strip out view code. }
New(anIconView);
end;
END;
{-------------------------------------------------------------------------------------------}
FUNCTION TIconApplication.DoMakeDocument(itsCmdNumber: CmdNumber): TDocument; OVERRIDE;
VAR
anIconDocument: TIconDocument;
BEGIN
New(anIconDocument); { Create a TIconDocument object. }
FailNIL(anIconDocument); { Make sure we were successful. }
anIconDocument.IIconDocument; { Initialize it. }
DoMakeDocument := anIconDocument; { Return a reference to the document. }
END;
{-------------------------------------------------------------------------------------------}
{----------------------------------TIconDocument methods------------------------------------}
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconDocument.IIconDocument;
VAR anIconBitMap : TIconBitMap;
BEGIN
fIconBitMap := NIL; { Set this to NIL so that if IDocument }
{ fails, TIconDocument.Free works okay. }
IDocument(kFileType, { This document's file type. }
kSignature, { This document's creator. }
kUsesDataFork, { This document does use the data fork }
NOT kUsesRsrcFork, { …but doesn't use the resource fork. }
NOT kDataOpen, { We don't want the data fork kept open }
NOT kRsrcOpen); { …nor the resource fork. }
New(anIconBitMap); { Allocate a new icon bitmap. }
FailNil(anIconBitMap); { Fail if we can't allocate the handle. }
anIconBitMap.IIconBitMap; { Initialize it. }
fIconBitMap := anIconBitMap; { Store a reference to it in a field. }
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconDocument.DoInitialState; OVERRIDE;
{ This method is called to set the document's data to the "new" state, as when the user }
{ chooses to open a new document instead of an existing one. We set the value of the }
{ document's icon bit map to that of a "seed" icon in our resource file. That way we }
{ can the document's initial state simply by changing the "seed" icon resource. }
VAR
seedIcon: Handle;
BEGIN
seedIcon := GetIcon(kSeedIconId); { Get the seed icon resource. }
FailNilResource(seedIcon);
fIconBitMap.SetIconBitMap(seedIcon)
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconDocument.Free; OVERRIDE;
BEGIN
FreeIfObject(fIconBitMap); { Dispose of the icon object if non-Nil.}
INHERITED Free;
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconDocument.DoMakeViews (forPrinting: BOOLEAN); OVERRIDE;
VAR
aWindow: TWindow;
BEGIN
aWindow := NewTemplateWindow(kIconWindowId, SELF); { Create the view hierarchy }
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconDocument.Fields (PROCEDURE DoToField (fieldName: Str255;
fieldAddr: Ptr;
fieldType: INTEGER)); OVERRIDE;
BEGIN
DoToField('TIconDocument', NIL, bClass);
DoToField('fIconBitMap', @fIconBitMap, bObject);
INHERITED Fields(DoToField);
END;
{-------------------------------------------------------------------------------------------}
{------------------------------------TIconView methods--------------------------------------}
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconView.IRes (itsDocument: TDocument; itsSuperView: TView; VAR itsParams: Ptr);
BEGIN
INHERITED IRes(itsDocument, itsSuperView, itsParams);
fMagnification := kDefaultMagnification;
fIconDocument := TIconDocument(itsDocument);
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconView.CalcMinSize (VAR minSize: VPoint); OVERRIDE;
BEGIN
minSize.h := kIconHBits * fMagnification + kBorder + kBorder;
minSize.v := kIconVBits * fMagnification + kBorder + kBorder;
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconView.Draw (area: Rect); OVERRIDE;
VAR
drawingRect: Rect;
BEGIN
SetRect(drawingRect, kBorder, kBorder,
kBorder + (kIconHBits * fMagnification),
kBorder + (kIconVBits * fMagnification));
fIconDocument.fIconBitMap.Draw(drawingRect);
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconView.Fields (PROCEDURE DoToField (fieldName: Str255;
fieldAddr: Ptr;
fieldType: INTEGER)); OVERRIDE;
BEGIN
DoToField('TIconView', NIL, bClass);
DoToField('fIconDocument', @fIconDocument, bObject);
DoToField('fMagnification', @fMagnification, bInteger);
INHERITED Fields(DoToFIeld);
END;
{-------------------------------------------------------------------------------------------}
{-----------------------------------TIconBitMap methods-------------------------------------}
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconBitMap.IIconBitMap;
BEGIN
fDataHandle := NewPermHandle(kIconSizeInBytes); { Allocate a handle for the bitmap. }
FailNil(fDataHandle); { Fail if we can't allocate the handle. }
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconBitMap.Free; OVERRIDE;
BEGIN
DisposIfHandle(fDataHandle); { dispose of icon data. }
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconBitMap.SetIconBitMap(theBitMap : Handle);
BEGIN
BlockMove(theBitMap^, fDataHandle^, { …then copy it into the document's }
kIconSizeInBytes) { …icon bitmap. }
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconBitMap.Clear;
VAR
iconAsLongArray: LongArrayHdl;
i: INTEGER;
BEGIN
iconAsLongArray := LongArrayHdl(fDataHandle); { Cast to array of longints. }
FOR i := 0 TO kMaxLong DO { Clear the bits 32 at a time. }
iconAsLongArray^^[i] := 0;
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconBitMap.Invert;
VAR
iconAsLongArray: LongArrayHdl;
i: INTEGER;
BEGIN
iconAsLongArray := LongArrayHdl(fDataHandle); { Cast to array of longints. }
FOR i := 0 TO kMaxLong DO { Invert the bits 32 at a time. }
iconAsLongArray^^[i] := BNOT(iconAsLongArray^^[i]);
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconBitMap.IconBitToWordBit (iconBit: Point; VAR word, bit: INTEGER);
{ This converts the given icon bit to a word and bit in an array of long words. }
VAR
bitNumber: INTEGER;
BEGIN
bitNumber := iconBit.v * kIconVBits + iconBit.h;
word := bitNumber DIV 32;
bit := 31 - (bitNumber MOD 32);
END;
{-------------------------------------------------------------------------------------------}
FUNCTION TIconBitMap.GetBit (iconBit: Point): BOOLEAN;
{ Returns the state of the given bit in the icon being drawn. }
VAR
word: INTEGER;
bitInWord: INTEGER;
BEGIN
IconBitToWordBit(iconBit, word, bitInWord);
GetBit := BTst(LongArrayHdl(fDataHandle)^^[word], bitInWord);
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconBitMap.SetBit (iconBit: Point; turnBitOn: BOOLEAN);
{ Set the state of the given bit in the icon being drawn. }
VAR
word: INTEGER;
bitInWord: INTEGER;
BEGIN
IconBitToWordBit(iconBit, word, bitInWord);
{$H-} { So the compiler thinks this is unsafe.}
IF turnBitOn THEN
BSet(LongArrayHdl(fDataHandle)^^[word], bitInWord)
ELSE
BClr(LongArrayHdl(fDataHandle)^^[word], bitInWord);
{$H+}
END;
{-------------------------------------------------------------------------------------------}
FUNCTION TIconBitMap.Copy: TIconBitMap;
VAR
copyOfIcon: TIconBitMap;
BEGIN
New(copyOfIcon); { Create a TIcon object. }
FailNIL(copyOfIcon); { Make sure we were successful. }
copyOfIcon.IIconBitMap; { Initialize it. }
copyOfIcon.SetIconBitMap(fDataHandle); { Copy the data. }
Copy := copyOfIcon; { Return a reference to the new handle. }
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconBitMap.Draw (area: Rect);
BEGIN
PlotIcon(area, fDataHandle);
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconBitMap.CopyDataTo (anIcon: TIconBitMap);
BEGIN
anIcon.SetIconBitMap(fDataHandle); { Copy data to the new icon. }
END;
{-------------------------------------------------------------------------------------------}
PROCEDURE TIconBitMap.Fields (PROCEDURE DoToField (fieldName: Str255;
fieldAddr: Ptr;
fieldType: INTEGER)); OVERRIDE;
BEGIN
DoToField('TIconBitMap', NIL, bClass);
DoToField('fDataHandle', @fDataHandle, bHandle);
INHERITED Fields(DoToField);
END;